package com.ymatou.iapi.godfreightmgr.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightTemplateBean;
import com.ymatou.iapi.godfreightmgr.parameter.FreightInfoVoBase;
import com.ymatou.iapi.godfreightmgr.parameter.GetTemplateListByIdListBean;
import com.ymatou.iapi.godfreightmgr.service.GetTemplateListByIdListCall;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.freight_template;
import com.ymttest.database.sqlwapper.godfreight.freight_templateWapper;
import com.ymttest.utils.AgentUsers;

/**
*运费模板列表（基本信息）
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetTemplateListByIdList{
	private static GetTemplateListByIdListBean gettemplatelistbyidlistBean;
	private static GetTemplateListByIdListCall gettemplatelistbyidlistCall;
	private static int sellerId = AgentUsers.rootUserId;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("运费模板列表（基本信息）");
	}

	@Before
	public void caseUp() {
		gettemplatelistbyidlistBean=new GetTemplateListByIdListBean();
		gettemplatelistbyidlistCall=new GetTemplateListByIdListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gettemplatelistbyidlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(List<Integer> templateIds){
		List<FreightInfoVoBase> templateList = gettemplatelistbyidlistCall.getTemplateList();
		freight_templateWapper templateWapper = new freight_templateWapper();
		templateList.forEach(p->{
			templateIds.forEach(x->{
				if (p.getTemplateId()==x){
					freight_template template = templateWapper.selectByPrimaryKey(x);
					Logger.verifyEquals(p.getSellerId(),template.getSellerId(),"验证sellerId");
					Logger.verifyEquals(p.getTemplateName(),template.getTemplateName(),"验证templateName");
					Logger.verifyEquals(p.getShareTemplate(),template.getIsShareTemplate(),"验证isShareTemplate");
					Logger.verifyEquals(p.getCurrency(),template.getCurrency(),"验证Currency");
				}
			});

		});
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateListByIdList_01() {
		Logger.start(true,"运费模板列表（基本信息），单个运费模板，不是共享模板，成功");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean freighttemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(),1,1);
			String templateId = GodfreightmgrService.CreateFreightTemplate(freighttemplateBean);

			List<Integer> templateList = new ArrayList<>();
			templateList.add(Integer.parseInt(templateId));
			gettemplatelistbyidlistBean.setTemplateIdList(templateList);
			gettemplatelistbyidlistCall.setData(gettemplatelistbyidlistBean);
			gettemplatelistbyidlistCall.callService();			
			Logger.verifyEquals("200",gettemplatelistbyidlistCall.getString("code"),"验证code");
			verifyDB(templateList);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateListByIdList_02() {
		Logger.start(true,"运费模板列表（基本信息），单个运费模板，共享模板，成功");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean freighttemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(),1,2);
			String templateId = GodfreightmgrService.CreateFreightTemplate(freighttemplateBean);

			List<Integer> templateList = new ArrayList<>();
			templateList.add(Integer.parseInt(templateId));
			gettemplatelistbyidlistBean.setTemplateIdList(templateList);
			gettemplatelistbyidlistCall.setData(gettemplatelistbyidlistBean);
			gettemplatelistbyidlistCall.callService();
			Logger.verifyEquals("200",gettemplatelistbyidlistCall.getString("code"),"验证code");
			verifyDB(templateList);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateListByIdList_03() {
		Logger.start(true,"运费模板列表（基本信息），多个运费模板，成功");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean freighttemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(),1,2);
			String templateId = GodfreightmgrService.CreateFreightTemplate(freighttemplateBean);

			freighttemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(),1,2);
			freighttemplateBean.setPolicyModel(1);
			String templateId1 = GodfreightmgrService.CreateFreightTemplate(freighttemplateBean);

			List<Integer> templateList = new ArrayList<>();
			templateList.add(Integer.parseInt(templateId));
			templateList.add(Integer.parseInt(templateId1));
			gettemplatelistbyidlistBean.setTemplateIdList(templateList);
			gettemplatelistbyidlistCall.setData(gettemplatelistbyidlistBean);
			gettemplatelistbyidlistCall.callService();
			Logger.verifyEquals("200",gettemplatelistbyidlistCall.getString("code"),"验证code");
			verifyDB(templateList);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateListByIdList_04() {
		Logger.start(true,"运费模板列表（基本信息），templateIdList为null，成功");
		try {
			gettemplatelistbyidlistBean.setTemplateIdList(new ArrayList<Integer>());
			gettemplatelistbyidlistCall.setData(gettemplatelistbyidlistBean);
			gettemplatelistbyidlistCall.callService();
			Logger.verifyEquals("400",gettemplatelistbyidlistCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}